import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Demo02 {
    public static void main(String[] args)throws Exception {
        //1.先创建一个DataSource
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/javatest?characterEncoding=utf8&useSSL=false");
        //数据库的用户名和密码
        ((MysqlDataSource) dataSource).setUser("root");
        ((MysqlDataSource) dataSource).setPassword("rootroot");
        //2.建立数据库服务器之间的连接，连接好以后才能进行后续的请求-响应交互
        Connection connection = dataSource.getConnection();
        //3.构造sql
        String sql1 = "select * from student";
        String sql = "insert into student values(?,?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,1);
        statement.setString(2,"张三");

/*        //4.发给服务器
        PreparedStatement statement = connection.prepareStatement(sql1);
        //4.发给服务器
        ResultSet resultSet = statement.executeQuery();
        //遍历
        while (resultSet.next()) {
            //针对某一行进行处理
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id:" + id + ",name:" + name);
        }*/
        //5.执行完后，释放资源，关闭连接(后创建的先释放)
   /*     resultSet.close();*/
        //5.执行完后，释放资源，关闭连接(后创建的先释放)
    /*    resultSet.close();*/
        statement.close();
        connection.close();
    }
}
